<!DOCTYPE html>

<html>
<head>
    <link href="css_anmi_load.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="../cordova.js"></script>
    <script type="text/javascript" src="index.js"></script>
    <script type="text/javascript" src="accessAuth_ajax.js"></script>
    <script type="text/javascript" src="../butterfly/vendor/jquery/jquery.js"></script>

    <meta charset="utf-8"/>
    <meta name="format-detection" content="telephone=no"/>
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
    <meta name="viewport"
          content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi"/>
    <meta name="msapplication-tap-highlight" content="no"/>
    <title>Hello World</title>
</head>
<body>
<div class="app">
    <div class="spinner">
    </div>

    <p id="tip_check_status">正在检测授权状态...</p>
    <!--<img src="img/splash.png">-->
    <div hidden id="set_ip">
        <p>请设置mdm服务器地址,当前值为</p>
        <input type="text" class="form-control" id="mdmHost" name="username" class="span3" value=""/>
        <input type="button" id="setDone" value="完成"/><br/>
        <textarea id="hint_input"></textarea><br/>
    </div>
</div>

<script type="text/javascript">
    app.initialize();

    var blockNextCommit = false;
    function succ() {
        setTimeout(function () {
            accessAuth.allOk();
        }, 1000);
    }

    function fail(errMsg) {
        blockNextCommit = false;
        setTimeout(function () {
            accessAuth.requireLogin();
        }, 1000);
    }

    document.addEventListener("deviceready", function () {
        if (accessAuth.isIpSetted()) {
            console.log("ip has setted");
            accessAuth.regAndCheckin(null, succ, fail, true);
            return;
        }

        console.log('ip not setted');
        $('#set_ip').show();
        $('#tip_check_status').hide();
        accessAuth.getMdmHost(function (err, ip) {
            var hint = err ? '当前值为空' + err : ip;
            $('#mdmHost').val(hint).show();
        });

        $('#setDone').click(function () {
            if (blockNextCommit) {
                return;
            }
            blockNextCommit = true;

            $('#hint_input').hide();
            var ip = $('#mdmHost').val();
            console.log("填写的 ip=" + ip);
            accessAuth.setMdmHost(ip, function (err) {
                if (err) {
                    console.log('设置mdm地址失败');
                    $('#mdmHost').val('');
                    $('#hint_input').val('设置ip失败:' + err).show();
                } else {
                    accessAuth.regAndCheckin(null, succ, fail, true);
                }
            });
        });
    });
</script>

</body>
</html>
